package com.common.sp;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Log;

import java.util.Set;

/**
 * SP封装类
 * Created by chenxiang on 2016/7/11.
 *
 * @author cx
 * @since v3.8
 */
public class SharedPreferUtil {

    private Context context = null;
    /*AppInfo，系统级数据，卸载APP时才需要清除的数据*/
    public static final String APP_INFO = "AppInfo";
    /*LoginInfo，登录级数据，退出登录时需要清除的数据 ， AppInfo*/
    public static final String LOGIN_INFO = "LoginInfo";

    /*用于清空今天以前的小区ID集合*/
    public static final String  PRECOMMID_INFO = "PreCommIDs";
    /*今天的小区ID集合*/
    public static final String  TODAY_COMMID_INFO = "TodayCommIDs";
//    /*小区ID对应的广告图片信息*/
//    public static final String  ADIMG_INFO = "AdImgInfo";


    private String[] spNames = {LOGIN_INFO, APP_INFO};

    private static SharedPreferUtil spInfo;

    private SharedPreferUtil() {

    }

    public static SharedPreferUtil getInstance() {
        if (spInfo == null) {
            spInfo = new SharedPreferUtil();
        }
//        Log.e("TAG1","SharedPreferUtil.getInstance()="+spInfo);
        return spInfo;
    }

    public void initSharePrefs(Context context) {
        this.context = context;
    }

    private String getFileName(String fileName) {
        if (TextUtils.isEmpty(fileName)) {
            return LOGIN_INFO;
        }
        return fileName;
    }

    public void putString(String key, String value) {
        /*默认SP文件*/
        putString(null, key, value);
    }

    public String getString(String key, String defValue) {
        return getString(null, key, defValue);
    }

    public void putString(String fileName, String key, String value) {
        SharedPreferences.Editor sp = context.getSharedPreferences(getFileName(fileName), Context.MODE_PRIVATE).edit();
        sp.putString(key, value);
        sp.commit();
    }

    public String getString(String fileName, String key, String defValue) {
        String file = getFileName(fileName);
        SharedPreferences sp = context.getSharedPreferences(file, Context.MODE_PRIVATE);

        return sp.getString(key, defValue);
    }

    public void putInt(String key, int value) {
        putInt(null, key, value);
    }

    public void getInt(String key, int defValue) {
        getInt(null, key, defValue);
    }

    public void putInt(String fileName, String key, int value) {
        SharedPreferences.Editor sp = context.getSharedPreferences(getFileName(fileName), Context.MODE_PRIVATE).edit();
        sp.putInt(key, value);
        sp.commit();
    }

    public int getInt(String fileName, String key, int defValue) {
        SharedPreferences sp = context.getSharedPreferences(getFileName(fileName), Context.MODE_PRIVATE);
        return sp.getInt(key, defValue);
    }

    public void putBoolean(String key, boolean value) {
        putBoolean(null, key, value);
    }

    public boolean getBoolean(String key, boolean defValue) {
        return getBoolean(null, key, defValue);
    }

    public void putBoolean(String fileName, String key, boolean value) {
        SharedPreferences.Editor sp = context.getSharedPreferences(getFileName(fileName), Context.MODE_PRIVATE).edit();
        sp.putBoolean(key, value);
        sp.commit();
    }

    public boolean getBoolean(String fileName, String key, boolean defValue) {
        SharedPreferences sp = context.getSharedPreferences(getFileName(fileName), Context.MODE_PRIVATE);
        return sp.getBoolean(key, defValue);
    }


    public void putLong(String fileName ,String key, Long value) {
        SharedPreferences.Editor sp = context.getSharedPreferences(getFileName(fileName), Context.MODE_PRIVATE).edit();
        sp.putLong(key, value);
        sp.commit();
    }

    public Long getLong(String fileName,String key, Long defValue) {
        SharedPreferences sp = context.getSharedPreferences(getFileName(fileName), Context.MODE_PRIVATE);
        return sp.getLong(key, defValue);
    }

    public void putStringSet(String fileName, String key, Set<String> value) {
        SharedPreferences.Editor sp = context.getSharedPreferences(getFileName(fileName), Context.MODE_PRIVATE).edit();
        sp.putStringSet(key, value);
        sp.commit();
    }

    public Set<String> getStringSet(String fileName, String key, Set<String> defValue) {
        SharedPreferences sp = context.getSharedPreferences(getFileName(fileName), Context.MODE_PRIVATE);
        return sp.getStringSet(key, defValue);
    }




    public void clearAll() {
        for (String name :
                spNames) {
            clear(name);
        }
    }

    public void clearLogin() {
        clear(LOGIN_INFO);
    }

    public void clear(String fileName) {
        SharedPreferences.Editor sp = context.getSharedPreferences(getFileName(fileName), Context.MODE_MULTI_PROCESS).edit();
        sp.clear();
        sp.commit();
    }



}
